package business.specialdiseasedata;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.esotericsoftware.yamlbeans.YamlException;
import common.Data;
import common.RunCaseJson;
import common.Yaml;
import io.restassured.RestAssured;
import io.restassured.response.Response;
import org.apache.commons.configuration.ConfigurationException;
import org.apache.log4j.Logger;
import utils.GetFileMess;
import utils.YamlReadUtil;
import java.io.FileNotFoundException;
import static common.Login.login;
import static common.TestStep.requestAndRespondBody;

public class CreateTemplate {

    Logger log = Logger.getLogger(business.specialdiseasedata.CreateTemplate.class);
    public static final String timestamp = String.valueOf(System.currentTimeMillis());
    public static final String system = "env.properties";
    Data data = login();

    /**
     *   专病大数据指标筛选增加template id
     * @param i  yaml的 datas第i行
     * @return
     * @throws ConfigurationException
     * @throws FileNotFoundException
     * @throws YamlException
     */

    public String CreateModel(int i) throws ConfigurationException, FileNotFoundException, YamlException {
        String path=new GetFileMess().getValue("specialdataPath",system);
        Yaml yaml= YamlReadUtil.getLocatorMap(path,this.getClass().getName());
        JSONObject json = new JSONObject();
        json.put("access_token", data.getAccessToken());
        json.put("authToken", data.getAccessToken());
        json.put("uniqu_no", data.getUniquNo());
        json.put("operatorId", data.getUserId());
        json.put("authUserId", data.getUserId());
        json.put("authName", data.getUserName());
/*        json.put("groupId", "100");
        json.put("timeScope", "0");    //
        json.put("status", "2");
        json.put("version", "5");    //版本：4数据目录筛选，5天塔筛选，6科研/临床/病例讨论筛选，7质控2.0  10事件筛选  11 队列筛选
        json.put("dataIds", "2887,248,249,2884,462,463,15524,15523,501410649331601408,894,895,896,897,899");
        json.put("whereResult", "{\"logicSymbol\":\"and\",\"whereList\":[{\"logicSymbol\":\"or\",\"whereType\":1,\"whereList\":[{\"logicSymbol\":\"or\",\"whereList\":[{\"symbol\":\"=\",\"dataValueType\":\"1\",\"dataId\":\"462\",\"dataRId\":\"484\",\"columnValue\":\"2015-01-01,2020-12-31\",\"columnName\":\"VISIT_DATE\",\"columnTitle\":\"入院时间\",\"columnType\":\"date\",\"dataName\":\"入院时间\"}]}]},{\"logicSymbol\":\"or\",\"whereType\":1,\"whereList\":[{\"logicSymbol\":\"or\",\"nextLogicSymbol\":\"or\",\"bracket\":\"start\",\"whereList\":[{\"symbol\":\"=\",\"dataValueType\":\"3\",\"dataId\":\"894\",\"dataRId\":\"795\",\"columnValue\":\"有\",\"columnName\":\"COUGH_STABLE\",\"columnTitle\":\"经常咳嗽\",\"columnType\":\"string\",\"dataName\":\"慢阻肺稳定期症状\"}]},{\"logicSymbol\":\"or\",\"nextLogicSymbol\":\"or\",\"whereList\":[{\"symbol\":\"=\",\"dataValueType\":\"3\",\"dataId\":\"895\",\"dataRId\":\"796\",\"columnValue\":\"有\",\"columnName\":\"EXPECTORATION_STABLE\",\"columnTitle\":\"经常咳痰\",\"columnType\":\"string\",\"dataName\":\"慢阻肺稳定期症状\"}]},{\"logicSymbol\":\"or\",\"nextLogicSymbol\":\"or\",\"whereList\":[{\"symbol\":\"=\",\"dataValueType\":\"3\",\"dataId\":\"896\",\"dataRId\":\"797\",\"columnValue\":\"有\",\"columnName\":\"WHEEZING_STABLE\",\"columnTitle\":\"经常喘息\",\"columnType\":\"string\",\"dataName\":\"慢阻肺稳定期症状\"}]},{\"logicSymbol\":\"or\",\"nextLogicSymbol\":\"or\",\"whereList\":[{\"symbol\":\"=\",\"dataValueType\":\"3\",\"dataId\":\"897\",\"dataRId\":\"798\",\"columnValue\":\"有\",\"columnName\":\"CHEST_TIGHTNESS_STABLE\",\"columnTitle\":\"经常胸闷\",\"columnType\":\"string\",\"dataName\":\"慢阻肺稳定期症状\"}]},{\"logicSymbol\":\"or\",\"bracket\":\"end\",\"whereList\":[{\"symbol\":\"=\",\"dataValueType\":\"3\",\"dataId\":\"899\",\"dataRId\":\"799\",\"columnValue\":\"有\",\"columnName\":\"PANTING_STABLE\",\"columnTitle\":\"活动后气促\",\"columnType\":\"string\",\"dataName\":\"慢阻肺稳定期症状\"}]}]}]}");
        json.put("dataScope", "1");      //	 类型：0全部，1住院，2门诊
        json.put("type", "0");     //类型：0初始模板，1队列模板，10数据目录模板，20数据集
        json.put("templateName", "初始版本" + timestamp);
        json.put("indexRule", "0");     //类型： 0全部，1第一次，2最后一次
        json.put("preview", "0");
        json.put("orgIds", "1286876463301992450,1289391474360504321,1289391555709030402,1289391602265804802,1286876491462549506,1286876520122228737,1286876063077310466,75637,1288768622175297538,1288768717914480642,1288769104457342977,1291283164377739266,1291306705864134657,76345,75636,75635");
        */

        json.put("groupId", yaml.getDatas().get(i).get("groupId"));
        json.put("timeScope", yaml.getDatas().get(i).get("timeScope"));    //
        json.put("status", yaml.getDatas().get(i).get("status"));
        json.put("version",yaml.getDatas().get(i).get("version"));    //版本：4数据目录筛选，5天塔筛选，6科研/临床/病例讨论筛选，7质控2.0  10事件筛选  11 队列筛选
        json.put("dataIds", yaml.getDatas().get(i).get("dataIds"));
        json.put("whereResult", yaml.getDatas().get(i).get("whereResult").toString());
        json.put("dataScope", yaml.getDatas().get(i).get("dataScope"));      //	 类型：0全部，1住院，2门诊
        json.put("type", yaml.getDatas().get(i).get("type"));     //类型：0初始模板，1队列模板，10数据目录模板，20数据集
        json.put("templateName", "初始版本" + timestamp);
        json.put("indexRule", yaml.getDatas().get(i).get("indexRule"));     //类型： 0全部，1第一次，2最后一次
        json.put("randomNum", yaml.getDatas().get(i).get("preview"));
        json.put("orgIds", yaml.getDatas().get(i).get("orgIds"));
        log.info(json.toString());
        Response response = RunCaseJson.runCase(json, "post", yaml.getUri());
        String jsonStr = response.asString();
        log.info("响应报文：" + jsonStr);
        String url = RestAssured.baseURI + ":" + RestAssured.port + RestAssured.basePath + yaml.getUri();
//        log.info("访问链接是："+url);
        requestAndRespondBody(url,json,jsonStr);

        JSONObject jsonObject = JSON.parseObject(jsonStr);
        String templateId = jsonObject.getJSONObject("data").getString("id");
        return templateId;   //返回templateid
    }

/*
* 获取病历列表的第一个患者id
* @Description: 
 * @param 	
* @return java.lang.String
* @author gujinle
* @date 2021-08-31 14:27
*/
    public String getpatientid() throws FileNotFoundException, ConfigurationException, YamlException {
        business.specialdiseasedata.CreateTemplate ct = new business.specialdiseasedata.CreateTemplate();
        String td = ct.CreateModel(0);
        String path=new GetFileMess().getValue("ymlPath",system);
        Yaml yaml= YamlReadUtil.getLocatorMap(path,"business.inhospitalqc.TemplateResultList");
        JSONObject json = new JSONObject();
        json.put("access_token", data.getAccessToken());
        json.put("authToken", data.getAccessToken());
        json.put("uniqu_no", data.getUniquNo());
        json.put("operatorId", data.getUserId());
        json.put("authUserId", data.getUserId());
        json.put("authName", data.getUserName());
        json.put("templateId",td);
        json.put("page",yaml.getDatas().get(0).get("page"));
        json.put("size",yaml.getDatas().get(0).get("size"));
        log.info(json.toString());
        Response response = RunCaseJson.runCase(json, "get", yaml.getUri());
        String jsonStr = response.asString();
        log.info("响应报文：" + jsonStr);
        JSONObject jsonObject = JSON.parseObject(jsonStr);
        JSONObject jo=jsonObject.getJSONObject("data");
        JSONObject object=jo.getJSONObject("dataPage");
        JSONArray contents=object.getJSONArray("content");
        JSONObject object1= (JSONObject) contents.get(1);
        String id=object1.getString("PATI_ID");
        return id;
    }



    public static void main(String[] args) throws FileNotFoundException, ConfigurationException, YamlException {
        business.specialdiseasedata.CreateTemplate ct = new business.specialdiseasedata.CreateTemplate();
        String td = ct.CreateModel(0);
        System.out.println(td);

    }
}
